package cn.kxm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.kxm.pojo.Company;
import cn.kxm.pojo.Part;
import cn.kxm.pojo.PartType;
import cn.kxm.pojo.SellOrderBill;
import cn.kxm.service.PartService;
import cn.kxm.service.SellOrderBillService;

@Controller
@RequestMapping("/sellorderbill")
public class SellOrderBillController extends BaseController{
	@Autowired
	@Qualifier("sellOrderBillServiceImpl")
    private SellOrderBillService sobs;
	@Autowired
	@Qualifier("partServiceImpl")
	private PartService ps;
	@RequestMapping("/findall")
	
	@ResponseBody
	public Object findAll(@RequestParam(value="findcode",required=false)String findcode,
			@RequestParam(value="findstartDate",required=false)String findstartDate,
			@RequestParam(value="findendDate",required=false)String findendDate,
			@RequestParam(value="findsupplierName",required=false)String findsupplierName,
			Integer page,Integer rows){
		List<SellOrderBill> list=sobs.getSellOrderBills(findcode, findstartDate, findendDate, findsupplierName, (page-1)*rows, rows);
		int total=sobs.getSellOrderBillCount(findcode, findstartDate, findendDate, findsupplierName);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("total",total);
		map.put("rows",list);
		return JSONArray.toJSONString(map);
	}
	
	@RequestMapping("/findallcompany")
	@ResponseBody
	public Object findAllCompany(@RequestParam(value="findName",required=false)String findName,
			@RequestParam(value="findCode",required=false)String findCode,
			@RequestParam(value="findType",required=false)Integer findType,
			Integer page,Integer rows){
		List<Company> list=sobs.getSuperlierList(findName, findCode, findType, (page-1)*rows, rows);
		int total=sobs.getSuperlierCount(findName, findCode, findType);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("total",total);
		map.put("rows",list);
		return JSONArray.toJSONString(map);
	}
	
	@RequestMapping("/findallpart")
	@ResponseBody
	public Object findAllPart(@RequestParam(value="partBrand",required=false)String partBrand,
			@RequestParam(value="partNo",required=false)String partNo,
			@RequestParam(value="partName",required=false)String partName,
			@RequestParam(value="partTypeId",required=false)Integer partTypeId,
			Integer page,Integer rows){
		
		List<Part> list=ps.getPartList(partBrand, partNo, partName, partTypeId, (page-1)*rows, rows);
		int total=ps.getPartCount(partBrand, partNo, partName, partTypeId);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("total",total);
		map.put("rows",list);
		return JSONArray.toJSONString(map);
	}
	
	@RequestMapping("/findallpartname")
    @ResponseBody
    public Object findAllTypeName(){
		
    	List<PartType> list=ps.getPartTypeList();
    	return JSON.toJSONString(list);
    }
}
